// pages/group/group.js
var g = require("../../utils/group")
Page({
  canjia(){
     var userid = this.data.userid;
     var username = this.data.username;
     var k = this.data.index;
     var matchid = this.data.result[k]._id;
     var matchname = this.data.result[k].title;
     var database = wx.cloud.database();
     database.collection("tb_baoming").add({
       data:{
         userid:userid,
         username:username,
         matchid:matchid,
         matchname:matchname
       }
     }).then((res)=>{
       if(res)wx.showToast({
         title: '报名成功',
       }).catch((err)=>{
         console.log(err)
       });
     })

  },
  shangchuan(){
    var that = this;
    wx.chooseMedia({
      count: 1,
      success(res) {
       
          console.log(res.tempFiles)
          var po = res.tempFiles[0].tempFilePath.lastIndexOf(".")
          var ext = res.tempFiles[0].tempFilePath.slice(po)
          wx.cloud.uploadFile({
              cloudPath: that.data.picname + ext,
              filePath: res.tempFiles[0].tempFilePath,
              success(res) {
                  console.log(res.fileID)
                 that.data.imageList.push(res.fileID)
                 that.setData({
                    imageList:that.data.imageList
                 });
              }
          })
      }
  })
     
  },
  changeCategory:function(e){
      this.setData({
        category:e.currentTarget.id
      });
  }, 
  shuru:function(e){
    this.setData({
      neirong:e.detail.value
    });
  },
  click(){
    var neirong = this.data.neirong;
    var groups = g.groups;
    var result = [];
    for(let i=0;i<groups.length;i++){
      var data = groups[i];
      if(data.name.match(neirong)){
        result.push(data);
      }
    }
    this.setData({
      result:result
    });
  },
  
    dianji:function(e){
      console.log("kkk:"+e.currentTarget.id)
      this.setData({
     index:e.currentTarget.id,
      flag:true
      })
    },
    sousuo(){
   var neirong = this.data.neirong;
   var database = wx.cloud.database();
   database.collection("tb_match").where({}).get().then((res)=>{
    var matches = res.data;
    var result =[];
    for (let i=0;i<matches.length;i++){
      var data = matches[i];
      if(data.title.match(neirong)){
        result.push(data)
      }
    }
    this.setData({
      result:result
    });
  }).catch((err)=>{
    console.log(err);
  });
   
  
    }, 
    cansai:function(e){
      var that = this;
      var k = e.currentTarget.id;
      var matchid = this.data.result[k]._id;
      var userid = this.data.userid;
      var database = wx.cloud.database();
      database.collection("tb_baoming").where({
        matchid:matchid
      }).get().then((res)=>{
        this.setData({
           xuanshou:res.data
        });
      }).catch((err)=>{
        console.log(err)
      });
     this.setData({
       index:e.currentTarget.id,
       biaozhi:true
     });
    },
      matchname:function(e){
         this.setData({
           matchname:e.detail.value
         });
      },
      picname:function(e){
         this.setData({
         picname:e.detail.value
         });
      },
      delete(){
        console.log(this.data.currentIndex);
         this.data.imageList.splice(this.data.currentIndex,1);
         this.setData({
           
           imageList:this.data.imageList,
           flag3:false
         });
      },
      choose:function(e){
        
        var k= e.currentTarget.id;
      console.log(this.data.imageList[k]);
        this.setData({
          flag3:true,
          currentImage:this.data.imageList[k],
          currentIndex:k+1
        });
      },
    /**
     * 页面的初始数据
     */
     data: {
       xuanshou:[],
       currentIndex:0,
       flag3:false,
       currentImage:null,
       imageList:[],
       picname:null,
      titles:g.list,
      category:2,
      neirong:null,
      result:[],
      index:0,
      flag:false,
      biaozhi:false,
      matchname:null,
      userid:null,
      username:null
    },
      create(){
        var that = this;
        var database = wx.cloud.database();
        var data ={
          title:this.data.matchname,
          userid:this.data.userid
        };
        database.collection("tb_match").add({
          data:{
            title:that.data.matchname,
          userid:that.data.userid,
          imageList:that.data.imageList
          }
        }).then((res)=>{
          console.log("插入一条数据后数据返回的数据："+res);
          if(res){
            wx.showToast({
              title: '成功插入数据',
            })
          }else{
            wx.showToast({
              title: '数据插入失败',
            })
          }
        }).catch((err)=>{
          console.log(err);  
        });
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      var that = this;
         wx.getStorage({
           key:"userid",
           success(res){
               that.setData({
                 userid:res.data
               });
               
               var database = wx.cloud.database();
               database.collection("tb_user").where({
                 _id:that.data.userid
               }).get().then((msg)=>{
                 that.setData({
                   username:msg.data[0].name
                 });
               }).catch((err)=>{
                 console.log(err)
               });
           }
         })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})